//------------------------------------------------------------------------------ 
// <copyright file="JTabs.cs" company="Fm Computer">
//     Copyright (c) Fm Computer di Menatti Fulvia.  All rights reserved.
// </copyright>
//----------------------------------------------------------------------------- 
[assembly: System.Web.UI.WebResource("FmjQuery.Controls.Extender.JTab.tab.css", "text/css")]

namespace FmjQuery
{
    using System.Linq;
    using System.Text;
    using System.Web.UI;

    /// <summary>
    /// Tabulation Control
    /// </summary>
    [ToolboxData("<{0}:JTabs runat=server></{0}:JTabs>")]
    [NonVisualControl]
    public class JTabs : JQueryBaseControl
    {
        #region Fields
        /// <summary>
        /// Collection of jTab
        /// </summary>
        private Tabs tlisttab;
        #endregion

        #region Costruct
        /// <summary>
        /// Initializes a new instance of the JTabs class.
        /// </summary>
        public JTabs()
        {
            this.IsRenderControl = true;
        }
        #endregion

        #region Property

        /// <summary>
        /// Gets list of tab
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public Tabs Tabs 
        { 
            get
            {
                if (this.tlisttab == null)
                {
                    this.tlisttab = new Tabs();
                }

                return this.tlisttab;
            }
        }

        /// <summary>
        /// Gets or sets id of selected tab
        /// </summary>
        public string SelectedID 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets a value indicating whether all tabs is unselect
        /// </summary>
        public bool UnSelectedAll 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets a value indicating whether animate change with collapse effect
        /// </summary>
        public bool Collapsible 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets name of event that active tab
        /// </summary>
        public string ClientActiveEvent 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets loading text for remote page
        /// </summary>
        public string LoadingText 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets a value indicating whether save into cache tabs
        /// </summary>
        public bool Cache 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets number of millisecond for change tab
        /// </summary>
        public int MilliSecondRotate 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets a value indicating whether that rotate effect is continuos
        /// </summary>
        public bool ContinueRotate 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets name of client function that call when select tab
        /// </summary>
        /// <example>
        ///     function(event, ui)
        ///     ui.tab     anchor element of the selected (clicked) tab
        ///     ui.panel   element, that contains the selected/clicked tab contents
        ///     ui.index   zero-based index of the selected (clicked) tab
        /// </example>
        public string ClientTabSelect
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets name of client function that call when load tab
        /// </summary>
        /// <example>
        ///     function(event, ui)
        ///     ui.tab     anchor element of the selected (clicked) tab
        ///     ui.panel   element, that contains the selected/clicked tab contents
        ///     ui.index   zero-based index of the selected (clicked) tab
        /// </example>
        public string ClientTabLoad
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets name of client function that call when show tab
        /// </summary>
        /// <example>
        ///     function(event, ui)
        ///     ui.tab     anchor element of the selected (clicked) tab
        ///     ui.panel   element, that contains the selected/clicked tab contents
        ///     ui.index   zero-based index of the selected (clicked) tab
        /// </example>
        public string ClientTabShow
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets name of client function that call when add tab
        /// </summary>
        /// <example>
        ///     function(event, ui)
        ///     ui.tab     anchor element of the selected (clicked) tab
        ///     ui.panel   element, that contains the selected/clicked tab contents
        ///     ui.index   zero-based index of the selected (clicked) tab
        /// </example>
        public string ClientTabAdd
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets name of client function that call when remove tab
        /// </summary>
        /// <example>
        ///     function(event, ui)
        ///     ui.tab     anchor element of the selected (clicked) tab
        ///     ui.panel   element, that contains the selected/clicked tab contents
        ///     ui.index   zero-based index of the selected (clicked) tab
        /// </example>
        public string ClientTabRemove
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets name of client function that call when enable tab
        /// </summary>
        /// <example>
        ///     function(event, ui)
        ///     ui.tab     anchor element of the selected (clicked) tab
        ///     ui.panel   element, that contains the selected/clicked tab contents
        ///     ui.index   zero-based index of the selected (clicked) tab
        /// </example>
        public string ClientTabEnable
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets name of client function that call when disable tab
        /// </summary>
        /// <example>
        ///     function(event, ui)
        ///     ui.tab     anchor element of the selected (clicked) tab
        ///     ui.panel   element, that contains the selected/clicked tab contents
        ///     ui.index   zero-based index of the selected (clicked) tab
        /// </example>
        public string ClientTabDisable
        {
            get;
            set;
        }
        #endregion

        #region Overrides of JQueryExtendControl
        /// <summary>
        /// Return list of webResource
        /// </summary>
        /// <returns>web resources</returns>
        public override JWebSources GetResources()
        {
            return new JWebSources { new JSource(Properties.Settings.Default.Css_Tab_Name, Properties.Settings.Default.Css_Tab_Url, JSourceType.css, true) };
        }

        /// <summary>
        /// Gets list of css image resources
        /// </summary>
        /// <returns>list of images</returns>
        public override CssResources GetCssResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of client nameSpace
        /// </summary>
        /// <returns>list of namespace</returns>
        public override ClientNameSpaces GetClientNameSpace()
        {
            return null;
        }

        /// <summary>
        /// Create jQuery control
        /// </summary>
        public override void CreateJControl()
        {
            if (this.Enabled)
            {
                this.Manager.Tab = true;

                // add child control
                foreach (JTab tab in this.tlisttab)
                {
                    this.Controls.Add(tab);
                }

                // create script
                StringBuilder builder = new StringBuilder("$(document).ready(function(){$('#");

                builder.AppendFormat("{0}').tabs(", this.ClientID);
                builder.Append("{");

                // selected tab
                int selectIndex = 0;
                if (this.UnSelectedAll || !string.IsNullOrEmpty(this.SelectedID))
                {
                    selectIndex = this.UnSelectedAll ? -1 : this.tlisttab.FindIndex(t => t.ID.Equals(this.SelectedID));
                }

                builder.AppendFormat("selected: {0}", selectIndex);

                // collapsible
                if (this.Collapsible)
                {
                    builder.AppendFormat(",collapsible: {0}", this.Collapsible.ToString().ToLower());
                }

                // event client
                if (!string.IsNullOrEmpty(this.ClientActiveEvent))
                {
                    builder.AppendFormat(",event : '{0}'", this.ClientActiveEvent);
                }

                // loading text
                if (!string.IsNullOrEmpty(this.LoadingText))
                {
                    builder.AppendFormat(",spinner: '{0}'", this.LoadingText);
                }

                // cache
                if (this.Cache)
                {
                    builder.AppendFormat(",cache: {0}", this.Cache.ToString().ToLower());
                }

                builder.Append("})");

                // disabled tab
                foreach (JTab tab in this.tlisttab.Where(t => !t.Enabled))
                {
                    builder.AppendFormat(".tabs('disable', {0})", this.tlisttab.IndexOf(tab));
                }

                // rotate tab
                if (this.MilliSecondRotate > 0)
                {
                    builder.AppendFormat(".tabs('rotate', {0}, {1})", this.MilliSecondRotate, this.ContinueRotate.ToString().ToLower());
                }

                // bind events (order : select, load, show)
                if (!string.IsNullOrEmpty(this.ClientTabAdd))
                {
                    builder.AppendFormat(".bind('tabsadd', {0})", this.ClientTabAdd);
                }

                if (!string.IsNullOrEmpty(this.ClientTabDisable))
                {
                    builder.AppendFormat(".bind('tabsdisable', {0})", this.ClientTabDisable);
                }

                if (!string.IsNullOrEmpty(this.ClientTabEnable))
                {
                    builder.AppendFormat(".bind('tabsenable', {0})", this.ClientTabEnable);
                }

                if (!string.IsNullOrEmpty(this.ClientTabLoad))
                {
                    builder.AppendFormat(".bind('tabsload', {0})", this.ClientTabLoad);
                }

                if (!string.IsNullOrEmpty(this.ClientTabRemove))
                {
                    builder.AppendFormat(".bind('tabsremove', {0})", this.ClientTabRemove);
                }

                if (!string.IsNullOrEmpty(this.ClientTabSelect))
                {
                    builder.AppendFormat(".bind('tabsselect', {0})", this.ClientTabSelect);
                }

                if (!string.IsNullOrEmpty(this.ClientTabShow))
                {
                    builder.AppendFormat(".bind('tabsshow', {0})", this.ClientTabShow);
                }

                builder.Append(";});");

                Utility.RegisterStartUpScript(this.Page, string.Concat("tabs_", this.ClientID), builder.ToString());
            }
        }
        #endregion

        #region Override
        /// <summary>
        /// Renders the control to the specified HTML writer.
        /// </summary>
        /// <param name="writer">The System.Web.UI.HtmlTextWriter object that receives the control content.</param>
        protected override void Render(HtmlTextWriter writer)
        {
            // render header
            new LiteralControl(string.Format("<div id='{0}'><ul>", this.ClientID)).RenderControl(writer);

            foreach (JTab tab in this.tlisttab)
            {
                tab.HeaderContainer.RenderControl(writer);
            }

            new LiteralControl("</ul>").RenderControl(writer);

            // render content
            foreach (JTab tab in this.tlisttab)
            {
                if (!tab.IsWebPage)
                {
                    new LiteralControl(string.Format("<div id='tb_{0}'>", tab.ClientID)).RenderControl(writer);
                    tab.ContentContainer.RenderControl(writer);
                    new LiteralControl("</div>").RenderControl(writer);
                }
            }

            new LiteralControl("</div>").RenderControl(writer);
        }
        #endregion
    }
}